package ch.fusun.baron.weather;

/**
 * Dissipates heat
 */
public class HeatDissipationUpdater implements AutomatonUpdater {

	private final int altitude;

	/**
	 * Constructor
	 * 
	 * @param altitude
	 */
	public HeatDissipationUpdater(int altitude) {
		this.altitude = altitude;
	}

	@Override
	public void update(Cube[][][] surroundingCubes, Cube cube) {
		float sum = 0;
		Cube oldCube = surroundingCubes[1][1][1];
		for (Cube[][] surroundingCube : surroundingCubes) {
			for (Cube[] elements : surroundingCube) {
				float diff = 0;
				for (Cube child : elements) {
					if (cube.getZ() == altitude - 1 && child.getZ() == 0) {

						diff = -oldCube.getTemperature();
					} else if (cube.getZ() == 0 && child.getZ() == altitude - 1) {
						// Do nothing
					} else// Default
					{
						diff = (child.getTemperature() - oldCube
								.getTemperature());
					}
					sum += diff;
				}
			}
		}

		cube.setTemperature(oldCube.getTemperature() + sum / 54.0f);
	}
}
